package com.jindi.jacococli.biz.business.service;

import com.alibaba.nacos.api.exception.NacosException;
import com.alibaba.nacos.api.naming.NamingService;
import com.alibaba.nacos.api.naming.pojo.Instance;
import com.alibaba.nacos.api.naming.pojo.ListView;
import com.jindi.jacococli.biz.constant.Constant;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

@Service
public class NacosFetchService {

    public static final Logger LOGGER = LoggerFactory.getLogger(NacosFetchService.class);

    @Resource
    private NamingService namingService;

    public ListView<String> serviceAll() {
        try {
            return namingService.getServicesOfServer(Constant.INT1, Integer.MAX_VALUE);
        } catch (NacosException e) {
            LOGGER.error("namingService.getServicesOfServer", e);
        }
        return null;
    }

    public List<Instance> instancesByServiceName(String serviceName) {
        // 获取服务的所有实例
        try {
            return namingService.getAllInstances(serviceName);
        } catch (NacosException e) {
            LOGGER.error("namingService.getAllInstances", e);
        }
        return null;
    }
}
